package com.bidevalution.controller.api;

import com.bidevalution.business.context.ParamsType;
import com.bidevalution.business.context.SysIdentityCode;
import com.bidevalution.business.service.*;
import com.bidevalution.controller.dto.BeSectionExpertInfoDTO;
import com.bidevalution.controller.util.RequestUtil;
import com.bidevalution.dao.entity.*;
import com.bidevalution.dao.mapper.BeSysParamsMapper;
import com.bidevalution.service.EntInfoService;
import com.bidevalution.support.context.Constants;
import com.bidevalution.support.context.RedisKeyConstants;
import com.bidevalution.support.util.StringUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.qtp.common.constants.SystemConstants;
import com.qtp.common.core.TSUser;
import com.qtp.common.data.RedisAccess;
import com.qtp.core.model.Response;
import com.qtp.core.redis.RedisUtil;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.IdsUtil;
import com.qtp.user.models.Role;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 修改记录
 * 2020-04-17 whc 分数检查增加环节是否已经初始化判断
 * 2020-05-07 夏云飞 需求 资信标评审有细项打分的需要所有专家都验证
 */

/**
 * 专家设置控制层
 * Created by glodon on 2019-08-08.
 */
@Log4j2
@RestController
@RequestMapping("/BeSectionExpertInfo")
@Api(tags = "标段专家设置")
public class BeSectionExpertInfoController extends BaseController{
    @Autowired
    private BeSectionExpertInfoService beSectionExpertInfoService;
    @Autowired
    private BeExpertSpecialityService beExpertSpecialityService;
    @Autowired
    private EntInfoService entInfoService;
    @Autowired
    private BeSectionInfoService beSectionInfoService;
    @Autowired
    private BeSysParamsMapper beSysParamsMapper;
    @Autowired
    private BeSysIdentityService beSysIdentityService;

    @Autowired
    private BeEvalutionOutcomeService beEvalutionOutcomeService;

    @Autowired
    private BeSectionMethodService beSectionMethodService;

    @Autowired
    private BeSectionStepService beSectionStepService;
    @Value("${expert.generateMobile}")
    private String generateMobile;
    @Value("${expert.identityThirdCheck}")
    private String identityThirdCheck;

    private static final String IMPORT_EXPERT_FAIL_FORMAT = "%s，未获取成功，原因：%s。";

    @RequestMapping("/getExpertSpecialityList")
    @ApiOperation(value = "专家设置-获取专家专业列表", response = BeExpertSpeciality.class, httpMethod = "GET")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "parentId", value = "父节点ID", required = true)
            })
    public Response<List<Map<String, Object>>> getExpertSpecialityList(@RequestParam("parentId") Integer parentId) {
        try {
            Map<String,Object> dataMap;
            List<Map<String, Object>> dataList = Lists.newArrayList();
            for(BeExpertSpeciality beExpertSpeciality:beExpertSpecialityService.selectChildList(parentId)) {
                dataMap = Maps.newHashMap();
                dataMap.put("value", beExpertSpeciality.getId());
                dataMap.put("label", beExpertSpeciality.getName());
                dataList.add(dataMap);
            }
            return Response.ok(dataList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("获取专家专业列表失败");
        }
    }
    @RequestMapping("/getExpertListBySectionId")
    @ApiOperation(value = "专家设置-根据标段ID获取专家信息列表", response = BeSectionExpertInfo.class, httpMethod = "GET")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段id", required = true)
            })
    public Response<List<BeSectionExpertInfo>> getExpertListBySectionId(@RequestParam("sectionId") String sectionId) {
        try {
            BeSysParams sysParams;
            BeExpertSpeciality expertSpeciality;
            Map<String,BeSysParams> sysParamsMap = Maps.newHashMap();
            for(BeSysParams beSysParams:beSysParamsMapper.selectForList(ParamsType.expertSource.name())){
                sysParamsMap.put(beSysParams.getSypaValue(), beSysParams);
            }
            Map<String,BeExpertSpeciality> expertSpecialityMap = Maps.newHashMap();
            for(BeExpertSpeciality beExpertSpeciality:beExpertSpecialityService.selectAllList()){
                expertSpecialityMap.put(beExpertSpeciality.getId().toString(), beExpertSpeciality);
            }
            List<BeSectionExpertInfo> beSectionExpertInfos = beSectionExpertInfoService.getExpertListBySectionId(sectionId,"0",null);
            for(BeSectionExpertInfo beSectionExpertInfo:beSectionExpertInfos) {
                sysParams = sysParamsMap.get(beSectionExpertInfo.getExpertSource()==null?"":beSectionExpertInfo.getExpertSource().toString());
                beSectionExpertInfo.setExpertSourceName(sysParams==null?"":sysParams.getSypaName());
                expertSpeciality = expertSpecialityMap.get(beSectionExpertInfo.getFirstSpeciality());
                beSectionExpertInfo.setFirstSpeciality(expertSpeciality==null?"":expertSpeciality.getName());

                expertSpeciality = expertSpecialityMap.get(beSectionExpertInfo.getSecondSpeciality());
                beSectionExpertInfo.setSecondSpeciality(expertSpeciality==null?"":expertSpeciality.getName());

                expertSpeciality = expertSpecialityMap.get(beSectionExpertInfo.getThirdSpeciality());
                beSectionExpertInfo.setThirdSpeciality(expertSpeciality==null?"":expertSpeciality.getName());

                expertSpeciality = expertSpecialityMap.get(beSectionExpertInfo.getFourSpeciality());
                beSectionExpertInfo.setFourSpeciality(expertSpeciality==null?"":expertSpeciality.getName());

            }
            return Response.ok(beSectionExpertInfos);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("根据标段ID获取专家信息列表失败");
        }
    }

    @RequestMapping("/getAllExpertListBySectionId")
    @ApiOperation(value = "专家设置-根据标段ID获取专家信息列表", response = BeSectionExpertInfo.class, httpMethod = "GET")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段id", required = true)
            })
    public Response<List<BeSectionExpertInfo>> getAllExpertListBySectionId(@RequestParam("sectionId") String sectionId) {
        try {
            BeSysParams sysParams;
            BeExpertSpeciality expertSpeciality;
            Map<String,BeSysParams> sysParamsMap = Maps.newHashMap();
            for(BeSysParams beSysParams:beSysParamsMapper.selectForList(ParamsType.expertSource.name())){
                sysParamsMap.put(beSysParams.getSypaValue(), beSysParams);
            }
            Map<String,BeExpertSpeciality> expertSpecialityMap = Maps.newHashMap();
            for(BeExpertSpeciality beExpertSpeciality:beExpertSpecialityService.selectAllList()){
                expertSpecialityMap.put(beExpertSpeciality.getId().toString(), beExpertSpeciality);
            }
            List<BeSectionExpertInfo> beSectionExpertInfos = beSectionExpertInfoService.getAllExpertListBySectionId(sectionId);
            for(BeSectionExpertInfo beSectionExpertInfo:beSectionExpertInfos) {
                sysParams = sysParamsMap.get(beSectionExpertInfo.getExpertSource()==null?"":beSectionExpertInfo.getExpertSource().toString());
                beSectionExpertInfo.setExpertSourceName(sysParams==null?"":sysParams.getSypaName());
                expertSpeciality = expertSpecialityMap.get(beSectionExpertInfo.getFirstSpeciality());
                beSectionExpertInfo.setFirstSpeciality(expertSpeciality==null?"":expertSpeciality.getName());

                expertSpeciality = expertSpecialityMap.get(beSectionExpertInfo.getSecondSpeciality());
                beSectionExpertInfo.setSecondSpeciality(expertSpeciality==null?"":expertSpeciality.getName());

                expertSpeciality = expertSpecialityMap.get(beSectionExpertInfo.getThirdSpeciality());
                beSectionExpertInfo.setThirdSpeciality(expertSpeciality==null?"":expertSpeciality.getName());

                expertSpeciality = expertSpecialityMap.get(beSectionExpertInfo.getFourSpeciality());
                beSectionExpertInfo.setFourSpeciality(expertSpeciality==null?"":expertSpeciality.getName());

            }
            return Response.ok(beSectionExpertInfos);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("根据标段ID获取专家信息列表失败");
        }
    }

    @RequestMapping("/saveExpertInfo")
    @ApiOperation(value = "专家设置-保存专家信息", response = Response.class, httpMethod = "POST")

    public Response saveExpertInfo(BeSectionExpertInfoDTO beSectionExpertInfoDTO) {
        try {

            return saveExpert(beSectionExpertInfoDTO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail(e.getMessage());
        }
    }

    @RequestMapping("/importExperts")
    @ApiOperation(value = "专家设置-从接口获取专家信息", response = Response.class, httpMethod = "POST")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段Id", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionCode", value = "标段编码", required = true)
            })
    public Response importExperts(@RequestParam("sectionCode") String sectionCode,@RequestParam("sectionId") String sectionId) {
//        try {
        return beSectionExpertInfoService.importExperts(sectionCode,sectionId);
//        } catch (ExpertException e) {
//            log.error("LOG[]importExperts_204[]{},error={}","异常日志", GsonUtil.toJsonWithGson(e));
//            Response response = Response.fail(e.getMessage());
//            response.setStatus(501);
//            return response;
//        } catch (Exception e) {
//            return Response.fail("获取专家失败，请稍后再试");
//        }
    }

    private Response saveExpert(BeSectionExpertInfoDTO beSectionExpertInfoDTO) throws Exception {
        BeSectionExpertInfo beSectionExpertInfo = new BeSectionExpertInfo();
        beSectionExpertInfo.setSeqId(IdsUtil.uuid());
        beSectionExpertInfo.setSectionId(beSectionExpertInfoDTO.getSectionId());
        beSectionExpertInfo.setCardNo(beSectionExpertInfoDTO.getCardNo());
        beSectionExpertInfo.setName(beSectionExpertInfoDTO.getName());
        beSectionExpertInfo.setLoginName(beSectionExpertInfoDTO.getLoginName());
        beSectionExpertInfo.setWorkUnit(beSectionExpertInfoDTO.getWorkUnit());
        try {
            if ("1".equals(generateMobile)) {
                String value = beSysIdentityService.getValue(SysIdentityCode.MobileCode);
                beSectionExpertInfo.setPhone(StringUtil.getLenString(value, 11, "0"));
            } else {
                beSectionExpertInfo.setPhone(beSectionExpertInfoDTO.getPhone());
            }
            beSectionExpertInfo.setExpertType(beSectionExpertInfoDTO.getExpertType());
            beSectionExpertInfo.setAreaType(beSectionExpertInfoDTO.getAreaType());
            beSectionExpertInfo.setIsAvoid(Constants.ZERO);
            beSectionExpertInfo.setIsManager(Constants.ZERO);
            beSectionExpertInfo.setCreateTime(DateUtil.now());
            beSectionExpertInfo.setUpdateTime(DateUtil.now());
            beSectionExpertInfo.setExpertSource(beSectionExpertInfoDTO.getExpertSource());
            List<BeSysParams> beSysParamList = beSysParamsMapper.selectForList(ParamsType.expertSource.name());
            for (BeSysParams beSysParams : beSysParamList) {
                if (beSysParams.getSypaValue().equals(String.valueOf(beSectionExpertInfo.getExpertSource()))) {
                    beSectionExpertInfo.setExpertSourceName(beSysParams.getSypaName());
                    break;
                }
            }
            beSectionExpertInfo.setFirstSpeciality(beSectionExpertInfoDTO.getFirstSpeciality());
            beSectionExpertInfo.setSecondSpeciality(beSectionExpertInfoDTO.getSecondSpeciality());
            beSectionExpertInfo.setThirdSpeciality(beSectionExpertInfoDTO.getThirdSpeciality());
            beSectionExpertInfo.setFourSpeciality(beSectionExpertInfoDTO.getFourSpeciality());
            beSectionExpertInfo.setSex(beSectionExpertInfoDTO.getSex());
            beSectionExpertInfo.setIsIdentity("1");
            //五库验证之前，需要保存标段专家信息（最初设计如此）
            if (!beSectionExpertInfoService.saveSectionExpertInfo(beSectionExpertInfo, beSectionExpertInfoDTO.getPassword())) {
                return Response.fail("保存标段专家信息失败");
            }
            log.info(identityThirdCheck + ":IdentityThirdCheck");
            if ("1".equals(identityThirdCheck)) {
                log.info(beSectionExpertInfoDTO + "**************IdentityThirdCheck");
                if (beSectionExpertInfoDTO.getExpertSource() == null) {
                    return Response.fail("专家来源不能为空");
                }
                if (StringUtils.isBlank(beSectionExpertInfoDTO.getCardNo())) {
                    return Response.fail("专家身份证号不能为空");
                }
                if (StringUtils.isBlank(beSectionExpertInfoDTO.getName())) {
                    return Response.fail("专家姓名不能为空");
                }
                //建委五库-方欣
                BeSectionInfo beSectionInfo = beSectionInfoService.getBeSectionInfoById(beSectionExpertInfoDTO.getSectionId());
                Response<String> response = entInfoService.getExportInfo(beSectionExpertInfoDTO.getCardNo(), beSectionExpertInfoDTO.getName(), beSectionInfo.getSectionCode());
                if (!response.isSuccess()) {
                    beSectionExpertInfoService.deleteBeSectionExpertInfos(beSectionExpertInfo.getSeqId());
                    return Response.fail(response.getError());
                }
            }
            if (!beSectionExpertInfoService.saveExpertInfo(beSectionExpertInfo, beSectionExpertInfoDTO.getPassword())) {
                return Response.fail("保存失败");
            }
            return Response.ok("保存成功");
        } catch(Exception e) {
            BeSectionExpertInfo exp = beSectionExpertInfoService.querySectionExpertInfoById(beSectionExpertInfo.getSeqId());
            if(exp!=null) {
                beSectionExpertInfoService.deleteBeSectionExpertInfos(beSectionExpertInfo.getSeqId());
            }
            throw e;
        }
    }

    private String getFirstSpeciality(String expertSpeciality) {
        String spec = "";
        if(StringUtils.isNotBlank(expertSpeciality)) {
            String firstSpeciality = expertSpeciality.split(" &gt; ")[0];
            BeExpertSpeciality speciality = beExpertSpecialityService.getSpecialityByName(firstSpeciality);
            if(speciality != null) {
                spec = speciality.getId().toString();
            }
        }
        return spec;
    }

    /**
     * 调用外部系统的api接口查询专家库
     * @param cardNo
     * @return
     */
    @RequestMapping("/queryExpertInfoByCardNo")
    @ApiOperation(value = "专家设置-根据身份证号码查询专家基本库信息", response = Response.class, httpMethod = "GET")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "cardNo", value = "身份证号码", required = true),
            })
    public Response<BeExpertInfo> queryExpertInfoByCardNo(@RequestParam("cardNo") String cardNo) {
        try {
            return Response.ok(beSectionExpertInfoService.queryExpertInfoByCardNo(cardNo));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("根据身份证号码查询专家基本库信息失败");
        }
    }

    @RequestMapping("/querySectionExpertInfoById")
    @ApiOperation(value = "专家设置-根据列表主键查询专家详细信息", response = BeSectionExpertInfo.class, httpMethod = "GET")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "seqId", value = "专家列表主键", required = true),
            })
    public Response<BeSectionExpertInfo> querySectionExpertInfoById(@RequestParam("seqId") String seqId) {
        try {
            return Response.ok(beSectionExpertInfoService.querySectionExpertInfoById(seqId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("根据列表主键查询专家详细信息失败");
        }
    }

    @RequestMapping("/getWebSocketAccessToken")
    @ApiOperation(value = "专家评标获取websocket访问权限的token", notes = "专家评标获取websocket访问权限的token", httpMethod = "GET")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段ID", required = true),
            })
    public Response<String> query(@RequestParam("sectionId") String sectionId) {
        try {
            String userGuid = RequestUtil.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            BeSectionExpertInfo expertInfo = beSectionExpertInfoService.selectByLoginName(sectionId, tsUser.getLoginName());
            if (expertInfo == null) {
                return Response.fail("sectionId:" + sectionId + ",loginName:" + tsUser.getLoginName() + ",不存在");
            }
            if (StringUtils.isBlank(RedisUtil.get(RedisKeyConstants.webSoketToken + expertInfo.getSeqId()))) {
                RedisUtil.set(RedisKeyConstants.webSoketToken + expertInfo.getSeqId(), expertInfo.getSeqId());
                RedisUtil.expire(RedisKeyConstants.webSoketToken + expertInfo.getSeqId(), 60 * 60 * 24);
            }
            return Response.ok(expertInfo.getSeqId());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("专家评标获取websocket访问权限的token失败");
        }
    }

    @RequestMapping("/deleteExpertInfos")
    @ApiOperation(value = "专家设置-根据主键删除专家信息", response = Response.class, httpMethod = "POST")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "seqIds", value = "专家列表主键（多个以逗号分割）", required = true)
            })
    public Response deleteBeSectionExpertInfos(@RequestParam("seqIds") String seqIds) {
        try {
            if(beSectionExpertInfoService.deleteBeSectionExpertInfos(seqIds)){
                return Response.ok("删除成功");
            } else {
                return Response.fail("删除失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("删除失败");
        }
    }

    @RequestMapping("/delExpertInfos")
    @ApiOperation(value = "专家设置-根据主键删除专家信息", response = Response.class, httpMethod = "POST")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "seqIds", value = "专家列表主键（多个以逗号分割）", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "remark", value = "删除原因", required = true)
            })
    public Response delBeSectionExpertInfos(@RequestParam("seqIds") String seqIds, @RequestParam("remark") String remark) {
        try {
            beSectionExpertInfoService.delBeSectionExpertInfos(seqIds, remark);
            return Response.ok("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("删除失败");
        }
    }

    /**
     * 保存专家回避信息并更新回避状态
     * @param sectionId
     * @param reason
     * @return
     */
    @PostMapping("/updateAvoidReason")
    @ApiOperation(value = "保存专家回避信息并更新回避状态(2019-09-17-v1)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "reason", value = "回避原因", dataType = "String", paramType = "query", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_ONE, message = "参数不可以为空"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_TWO, message = "获取专家信息失败，请重新登录"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_THREE, message = "保存失败，请重新保存"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_FOUR, message = "保存异常"),
    })
    public Response updateAvoidReason(@RequestParam("sectionId") String sectionId, @RequestParam("reason") String reason) {
        if (StringUtils.isBlank(sectionId) || StringUtils.isBlank(reason)) {
            return Response.fail("参数不可以为空", Constants.FIVE_HUNDRED_AND_ONE);
        }
        String userGuid = RequestUtil.getHeader("userGuid");
        TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
        if (tsUser != null) {
            BeSectionExpertInfo beSectionExpertInfo = new BeSectionExpertInfo();
            beSectionExpertInfo.setLoginName(tsUser.getLoginName());
            beSectionExpertInfo.setSectionId(sectionId);
            beSectionExpertInfo.setAvoidReason(reason);
            //是否回避(1是 0否)
            beSectionExpertInfo.setIsAvoid(Constants.ONE);
            beSectionExpertInfo.setIsManager(Constants.ZERO);
            beSectionExpertInfo.setUpdateTime(DateUtil.now());
            try {
                if (beSectionExpertInfoService.updateAvoidReason(beSectionExpertInfo)) {
                    return Response.ok("保存成功");
                }
                return Response.fail("保存失败，请重新保存", Constants.FIVE_HUNDRED_AND_THREE);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Response.fail("保存异常", Constants.FIVE_HUNDRED_AND_FOUR);
            }
        } else {
            return Response.fail("获取专家信息失败，请重新登录", Constants.FIVE_HUNDRED_AND_TWO);
        }
    }

    /**
     * 根据专家登录账号查询专家回避状态
     *
     * @param sectionId
     * @return
     */
    @GetMapping("/queryExpertInfoByLoginName")
    @ApiOperation(value = "根据专家登录账号查询专家信息(2019-11-24-v1)", response = BeSectionExpertInfo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", dataType = "String", paramType = "query", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = Constants.TWO_HUNDRED, message = "专家信息"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_ONE, message = "参数不可以为空"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_TWO, message = "获取专家信息失败，请重新登录"),
    })
    public Response<BeSectionExpertInfo> queryExpertInfoByLoginName(@RequestParam("sectionId") String sectionId) {
        if (StringUtils.isBlank(sectionId)) {
            Response.fail("参数不可以为空", Constants.FIVE_HUNDRED_AND_ONE);
        }
        String userGuid = RequestUtil.getHeader("userGuid");
        TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
        if (tsUser != null) {
            return Response.ok(beSectionExpertInfoService.selectByLoginName(sectionId, tsUser.getLoginName()));
        } else {
            return Response.fail("获取专家信息失败，请重新登录", Constants.FIVE_HUNDRED_AND_TWO);
        }
    }


    @GetMapping("/isEnterReview")
    @ApiOperation(value = "根据专家登录账号查询专家是否可以进入评审(2019-11-11-v1)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", dataType = "String", paramType = "query", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_ONE, message = "参数不可以为空"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_TWO, message = "获取专家信息失败，请重新登录"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_THREE, message = "获取信息失败，请联系管理员"),
    })
    public Response<String> isEnterReview(@RequestParam("sectionId") String sectionId) {
        try {
            if (StringUtils.isBlank(sectionId)) {
                Response.fail("参数不可以为空", Constants.FIVE_HUNDRED_AND_ONE);
            }
            String userGuid = RequestUtil.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            if (tsUser != null) {
                return Response.ok(beSectionExpertInfoService.isEnterReview(sectionId, tsUser.getLoginName()));
            } else {
                return Response.fail("获取专家信息失败，请重新登录", Constants.FIVE_HUNDRED_AND_TWO);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("获取信息失败，请联系管理员", Constants.FIVE_HUNDRED_AND_THREE);
        }
    }
    @GetMapping("/isExpert")
    @ApiOperation(value = "根据专家登录账号判断是否是专家权限")
    @ApiResponses({
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_TWO, message = "获取专家权限信息失败，请重新登录"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_THREE, message = "获取信息失败，请联系管理员"),
    })
    public Response isExpert() {
        try {
            String userGuid = RequestUtil.getHeader("userGuid");
            List<Role> roleList = RedisAccess.getList(Role.class, String.format("%s_%s", SystemConstants.ROLE_SESSION, userGuid));
            if (roleList != null) {
                Boolean flag = false;
                for (Role role : roleList) {
                    if ("专家".equals(role.getRoleName())) {
                        flag = true;
                        break;
                    }
                }
                return Response.ok(flag);
            } else {
                return Response.fail("获取专家权限信息失败，请重新登录", Constants.FIVE_HUNDRED_AND_TWO);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("获取信息失败，请联系管理员", Constants.FIVE_HUNDRED_AND_THREE);
        }
    }




    @GetMapping("/checkScore")
    @ApiOperation(value = "检查技术评分和资信评分", response = BeSectionEnterprise.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "sectionId", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "sectionStepId", value = "环节ID", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "layoutCode", value = "显示布局Code", dataType = "String", paramType = "query", required = true)
    })
    public Response updateEnterpriseName(
            @RequestParam("sectionId")String sectionId,
            @RequestParam("sectionStepId") String sectionStepId,
            @RequestParam("layoutCode") String layoutCode) {
        try {
            String userGuid = RequestUtil.getHeader("userGuid");
            TSUser tsUser = RedisAccess.getValue(TSUser.class, userGuid);
            BeSectionExpertInfo beSectionExpertInfo = beSectionExpertInfoService.selectByLoginName(sectionId, tsUser.getLoginName());
            String exprtId = beSectionExpertInfo.getSeqId();
            //查询当前标段流程的版本号submitCode
            BeSectionInfo beSectionInfo = beSectionInfoService.getBeSectionInfoById(sectionId);
            BeSectionMethod beSectionMethod = beSectionMethodService.selectBySectionId(sectionId);
            BeEvalutionOutcome beEvalutionOutcome = new BeEvalutionOutcome();
            beEvalutionOutcome.setSectionStepId(sectionStepId);
            beEvalutionOutcome.setLayoutCode(layoutCode);
            beEvalutionOutcome.setExpertId(exprtId);
            beEvalutionOutcome.setSubmitCode(beSectionMethod.getSubmitCode());
            beEvalutionOutcome.setSectionId(sectionId);
            beEvalutionOutcome.setIsBidHide(beSectionMethod.getIsBidHide());
            beEvalutionOutcome.setProjectID(beSectionInfo.getTdProjectid());

            BeSectionStep beSectionStep =beSectionStepService.getSectionStepBySeqId(sectionStepId);
            List<BeEvalutionOutcomeVo> list = new ArrayList<>();

            List<BeEvalutionOutcome> beEvalutionOutcomeList = beEvalutionOutcomeService.findBeEvalutionOutcomeByEnterprise(sectionId, sectionStepId);
            /** begin:2020-05-07 夏云飞 需求 资信标评审有细项打分的需要所有专家都验证 */
//            BeSectionExpertInfo expertInfo = beSectionExpertInfoService.querySectionExpertInfoById(exprtId);
            if (beSectionStep.getStepName().indexOf("资信") > -1) {
                if (beEvalutionOutcomeList.size() == 0) {
                    return Response.fail("请先进行相关评审!");
                }
                // 组长才检查
                list = beEvalutionOutcomeService.findC1IsNullByZiXin(beEvalutionOutcome);
                /** end:2020-05-07 夏云飞 需求 资信标评审有细项打分的需要所有专家都验证 */
            } else {
                if (beEvalutionOutcomeList.size() == 0) {
                    return Response.fail("请先进行相关评审!");
                }
                list = beEvalutionOutcomeService.findC1IsNullByJiShu(beEvalutionOutcome);
            }

            return Response.ok(list);
        } catch (Exception e) {
            log.error(e);
            return Response.fail("查询失败");
        }
    }
}
